package com.portal.sso.server.impl;


import cn.hutool.core.util.StrUtil;
import com.portal.sso.core.config.PortalResault;
import com.portal.sso.core.config.RespoingModelEnum;
import com.portal.sso.core.config.SsoConfig;
import com.portal.sso.core.exception.WhitelistException;
import com.portal.sso.core.model.WhitelistModel;
import com.portal.sso.core.server.AbstractTokenManage;
import com.portal.sso.server.processor.ServerWhitelistProcessor;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

@AllArgsConstructor
public class SimpleTokenManageImpl extends AbstractTokenManage {


    ServerWhitelistProcessor serverWhitelistProcessor;

    @Override
    public String GetSecret(String appId) throws WhitelistException{
        try {
            if (appId != null) {
                WhitelistModel model = serverWhitelistProcessor.getList()
                        .stream()
                        .filter(t -> t.getAppId().equals(appId))
                        .limit(1)
                        .collect(Collectors.toList()).get(0);
                if (model == null) return null;
                return model.getAppSecret();
            } else {
                return SsoConfig.SSO_APP_DEFAULT_SECRET;
            }
        }catch (Exception e){
           throw  new WhitelistException(PortalResault.ERR_SSO_NOT_WHITELIST.desc());
        }


    }

    @Override
    public String GetUriStyle(String appId) {
        //默认风格
        String uri_style=SsoConfig.SSO_REQUEST_URI_TYPE_REST;
        if (StrUtil.isEmpty(appId)) {
            return uri_style;
        }
        //获得白名单
        WhitelistModel item = serverWhitelistProcessor.getList().stream()
                .filter(t -> t.getAppId().equals(appId))
                .collect(Collectors.toList())
                .get(0);
        //没有查到注册的系统信息时返回
        if (item == null||StrUtil.isEmpty(item.getUriStyle())) {
            return uri_style;
        }else{
            return item.getUriStyle();
        }
    }

    @Override
    public RespoingModelEnum GetRepModel(String appId) {
        //默认风格
        if (StrUtil.isEmpty(appId)) {
            return RespoingModelEnum.WEB;
        }
        //获得白名单
        WhitelistModel item = serverWhitelistProcessor.getList().stream()
                .filter(t -> t.getAppId().equals(appId))
                .collect(Collectors.toList())
                .get(0);
        //没有查到注册的系统信息时返回
        if (item == null||StrUtil.isEmpty(item.getResModel())) {
            return RespoingModelEnum.WEB;
        }else{
            return item.getUriStyle().equals(SsoConfig.SSO_RESPONSE_MODEL_WEB)?RespoingModelEnum.WEB:RespoingModelEnum.JSON;
        }
    }

    /**
     * 检查请求是否合法
     * @param appId
     * @param server 申请jwt的server的ip或域名
     * @return
     */
    @Override
    public boolean CheckAppId(String appId, String server) {
        if (StrUtil.isEmpty(appId) || StrUtil.isEmpty(server)) {
            return false;
        }
        //获得白名单
        WhitelistModel item = serverWhitelistProcessor.getList().stream()
                .filter(t -> t.getAppId().equals(appId))
                .collect(Collectors.toList())
                .get(0);
        //没有查到注册的系统信息时返回
        if (item == null) {
            return false;
        }
        //没有注册白名单时返回true
        if(StrUtil.isEmpty(item.getWhitelist())){return true;}
        //白名单包含server时返回true
       if( item.getWhitelist().contains(server)){
           return true;
       }

        return false;
    }
}
